package henu.javaweb.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.nio.charset.Charset;
import java.time.DayOfWeek;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import henu.javaweb.bean.Student;
import henu.javaweb.dao.DaoFactory;



/**
 * Servlet implementation class StudentLoginServlet
 */
@WebServlet("/StudentLoginServlet")
public class StudentLoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;Charset=utf-8");
		PrintWriter out=response.getWriter();
		
		String sno = request.getParameter("sno");
		String spass = request.getParameter("spassword");
			
		//String MD5Password=henu.javaweb.util.MD5.getResult(spass);
		Student student=DaoFactory.getStudentDaoInstance().login(sno,spass);
		
		if(student!=null) {
			request.getSession().setAttribute("sno",student.getS_no());
			response.sendRedirect("jsp/student/main.jsp");
		}
		else {
			out.print("<script type=\"text/javascript\">alert('账号或密码有误!');history.go(-1);</script>");
		}
	}

}
